package com.gm.census.controller;

import java.util.Collections;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheFactory;
import net.sf.jsr107cache.CacheManager;

import com.gm.census.service.ServiceFactory;
import com.gm.census.service.UserInfoFacade;

public abstract class BaseController {
	private UserInfoFacade userInfo;
	private Cache cache;
	private ServiceFactory fact;

	public BaseController(ServiceFactory fact, UserInfoFacade userInfo) {
		this.fact = fact;
		this.userInfo = userInfo;
	}

	protected ServiceFactory services() {
		return fact;
	}

	protected UserInfoFacade userInfo() {
		return userInfo;
	}

	public Cache getCache() {
		if (cache == null) {
			try {
				CacheFactory cacheFactory = CacheManager.getInstance()
						.getCacheFactory();
				cache = cacheFactory.createCache(Collections.emptyMap());
			} catch (CacheException e) {
				throw new RuntimeException(e);
			}
		}
		return cache;
	}

}
